﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using HRM.Models;
using DTO;
namespace HRM.api
{
    public class KN_MEMAPIController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<KY_NANG_MEM> Get()
        {
            return new WebData().KY_NANG_MEMs.GetAll();
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        public HttpResponseMessage Post(KY_NANG_MEM item)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    WebData model = new WebData();
                    model.KY_NANG_MEMs.InsertOnSubmit(ref item);
                    var response = Request.CreateResponse<KY_NANG_MEM>(HttpStatusCode.Created, item);
                    return response;
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Lỗi thêm dữ liệu");
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

        }
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, KY_NANG_MEM item)
        {
            try
            {
                WebData model = new WebData();
                model.KY_NANG_MEMs.UpdateOnSubmit(item);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // DELETE api/<controller>/5
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                WebData model = new WebData();
                KY_NANG_MEM item = new KY_NANG_MEM();
                item.ID = id;
                model.KY_NANG_MEMs.DeleteOnSubmit(item);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}